package lingou.homework.postbar.controller.tool;

import lingou.homework.postbar.controller.proto.resp.Response;

import java.util.function.Function;
import java.util.regex.Pattern;

public class FormatHelper {

    public static Response illegalPassword(String password){
        if(password == null || password.length() < 6 || password.length() > 18)
            return Response.ofFailure("请输入6-18位密码！");
        return null;
    }

    public static Response illegalEmail(String str, Function<String, Boolean> exists){
        if(str == null || str.length() == 0)
            return Response.ofFailure("邮箱不能为空！");
        if(exists.apply(str))
            return Response.ofFailure("邮箱已存在！");
        return null;
    }

    public static Response illegalUsername(String str){
        if(str == null || str.length() == 0)
            return Response.ofFailure("用户名不能为空！");
        if(str.length() > 30)
            return Response.ofFailure("用户名不能超过30个字！");
        return null;
    }

    public static Response illegalIdentity(String name, String studentId){
        if(name == null || name.length() == 0)
            return Response.ofFailure("姓名不能为空！");
        if(name.length() > 30)
            return Response.ofFailure("姓名不能超过30个字！");
        if(studentId == null || studentId.length() == 0)
            return Response.ofFailure("学号不能为空！");
        if(!Pattern.matches("\\d{8}", studentId))
            return Response.ofFailure("请输入8位数字学号！");
        return null;
    }

}
